/*
 * =====================================================================================
 *
 *       Filename:  Mutex.h
 *
 *    Description:  wrapper for mutex
 *
 *        Version:  1.0
 *        Created:  2012年06月21日 10时15分33秒
 *       Revision:  none
 *       Compiler:  gcc/g++
 *
 *         Author:  wangtengchao , wangtengchao AT gmail.com
 *   Organization:  
 *
 * =====================================================================================
 */
#ifndef MUTEX_H
#define MUTEX_H
#include<pthread.h>
#include<boost/noncopyable.hpp>
#include<cstdio>

namespace inm
{
	namespace concurrency
	{
		class MutexLock : boost::noncopyable
		{
			public:
				MutexLock()
				{
					pthread_mutex_init(&mutex_,NULL);
				}
				~MutexLock()
				{
					pthread_mutex_destroy(&mutex_);
				}
				void lock()
				{
					pthread_mutex_lock(&mutex_);
				}
				void unlock()
				{
					pthread_mutex_unlock(&mutex_);
				}
				pthread_mutex_t* getPthreadMutex()
				{//for Condition's function pthread_con_wait
					return &mutex_;
				}
			private:
				pthread_mutex_t mutex_;
		};
		class MutexLockGuard : boost::noncopyable
		{
			public:
				explicit MutexLockGuard(MutexLock& mutex):mutexLock_(mutex)
				{
					mutexLock_.lock();
				}
				~MutexLockGuard()
				{
					mutexLock_.unlock();
				}
			private:
				MutexLock& mutexLock_;//note: reference not MutexLock

		};

	//print error when call MutexLockGuard(mutex_).Correct action is MutexLockGuard mutexGuard(mutex_)
	#define MutexLockGuard(x) printf("MutexLockGuard(x) error!")  
	};
};

#endif //MUTEX_H
